// components/navbar/navbar.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title:{  //标题文字
      type: String
    },
    showPersonal:{ //是否显示个人中心按钮
      type:Boolean,
      default:true
    },
    showArrow:{  //是否显示返回箭头
      type:Boolean,
      default:true
    },
    personPicker:{ //是否显示picker选择
      type:Boolean,
      default:false
    },
    pickerData:{  //picker的数据
      type:Array,
      default:[]
    },
    rangeKey:{  //picker显示的键名
      type:String,
      default:''
    },
    rangeValue:{
      type:String,
      default:''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    /* 用户信息 */
    userInfo_share:{},
    /*  */
    navH:'',   //导航栏高度
    pickerData:[],
    pickerName:'',
    pickerValue:'',
    selectedPickerValue:''
  },
  /*  */
  lifetimes:{
    created:function(){
    },
    attached:function(){
      this.setData({
        userInfo_share:app.globalData.get_userInfo(),
        navH: app.globalData.navHeight
      });
      //console.log(this.data.userInfo_share);
      //
      if(this.data.personPicker){
        let timer = null,timer2 = null;;
        clearInterval(timer);
        timer = setInterval(()=>{
          if(this.data.pickerData.length>0){
            this.setData({
              pickerName:this.data.pickerData[0].visit_name
            });
            app.globalData.visit_userid = this.data.pickerData[0].visit_userid
            clearInterval(timer);
          }
        },100);
        //
        clearTimeout(timer2);
        timer2 = setTimeout(()=>{
          if(!this.data.pickerData.length){
            clearInterval(timer);
            this.setData({
              pickerName:'微米享名片'
            });
          }
        },5000)
      }
    },
    ready:function(){
    }
  },
  /*  */
  pageLifetimes:{
    show:function(){
      this.setData({
        userInfo_share:app.globalData.get_userInfo()
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /* 根据是客户还是销售跳转到对应的页面 0销售 1客户 */
    gotoPersonalCenter:function(){
      let state = getApp().globalData.isCustomerOrSaler.call(this);
      //---是销售
      if(state=='0'){
        wx.navigateTo({
          url:'/pages/employee/employee'
        })
      }
      //---是客户
      if(state=='1'){
        wx.navigateTo({
          url:'/pages/personal/personal'
        })
      }
    },
    /* 返回上一页 */
    navigateBack: function(){
      let pages = getCurrentPages();
      //console.log(pages);
      //支付成功的页面返回时--返回产品页
      if(pages[pages.length-1].route==='pages/paySuccess/paySuccess'){
        wx.switchTab({
          url: '/pages/product/product',
        })
        return;
      }
      wx.navigateBack();
    },
    /* 选择器 */
    pickerSelect:function(e){
      console.log(e.detail.value);
      //
      if(this.data.rangeValue){
        this.setData({
          pickerName:this.data.pickerData[e.detail.value*1][this.data.rangeKey],
          selectedPickerValue:this.data.pickerData[e.detail.value*1][this.data.rangeValue]
        })
      }else{
        this.setData({
          selectedPickerValue:this.data.pickerData[e.detail.value*1]
        })
      }
      //
      console.log(this.data.selectedPickerValue);
      this.triggerEvent('tabUser',{
        [this.data.rangeValue]:this.data.selectedPickerValue
      });
    }
  }
})
